﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Utilities;
using System.Data.SqlClient;
using SpringWaterDataAccess;

namespace client
{
	/// <summary>
	/// Summary description for bloggerweb.
	/// </summary>
	public partial class bloggerweb : System.Web.UI.Page
	{

		protected string cateID = "";
		protected int catid=0;
		protected string postID = "";
		protected int totalRecords = 0;	
		protected int pageNumber = 0;
		protected int totalPages = 0;
		protected string userID = "";
		protected int year = 0;
		protected int month = 0;
		protected int day = 0;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			cateID = Request.QueryString["CategoryID"];
			postID = Request.QueryString["PostID"];
			userID = Request.QueryString["UserID"];
			year = Convert.ToUInt16(Request.QueryString["Year"]);
			month =  Convert.ToUInt16(Request.QueryString["Month"]);
			day = Convert.ToUInt16(Request.QueryString["Day"]);

			pageNumber = Convert.ToInt16(Request.QueryString["Page"]);
            
			if (cateID!="" && cateID!=null)
			{
				catid = Convert.ToUInt16(cateID);
			}
			if (Utilities.Helper.IdentifyUser(Request)) 
			{
				LinkButton1.Visible = true;
				LinkButton2.Visible = true;
				LinkButton3.Visible = true;
			} 
			else 
			{
				LinkButton1.Visible = false;
				LinkButton2.Visible = false;
				LinkButton3.Visible = false;
			}

			// If page number is not passed, treat as the first page
			if (pageNumber == 0)
				pageNumber=1;

			// Put user code to initialize the page here
			if (!IsPostBack) 
			{
				BindData();		
			}
		}


		private void BindData() 
		{

			ForumDataAccess da = new ForumDataAccess();
//            if(cateID="")
			int uid = Convert.ToUInt16(Request.QueryString["UserID"]);
			DataList1.DataSource =  da.getPosts(
				uid, year,month, day,1, 10, out totalRecords,
				        "blog", "", catid, "CreateDate DESC");
			DataList1.DataBind();

			DataList2.DataSource = da.getRepliesByUserID(uid, 1, 10, 
				out totalRecords, "blog","", catid, "CreateDate DESC");
			DataList2.DataBind();

			int startRecordNumber = (pageNumber - 1)*10 + 1;

			DataSet MyRepeaterDataSource;
				
			// if null, we are displaying user's list of articles
			if (postID == null || postID == "") 
			{				 
			    MyRepeaterDataSource  = da.getPosts(uid, year,month,day,
					                     startRecordNumber, 10, out totalRecords,
					                           "blog","", catid, "CreateDate DESC");
				
				totalPages =  totalRecords / 10 + 1;
			} 
			else // display the article itself
			{
				int pid = Convert.ToUInt16(Request.QueryString["PostID"]);
				da.IncreaseClicks(pid); 
				
				DataList4.DataSource = da.getRepliesByPostID(pid,0,0,
					out totalRecords,"blog", "", catid, "CreateDate");

				DataList4.DataBind();

				MyRepeaterDataSource = da.GetPostDetailByPostID(postID);
				
			}

			// We need to get reply numbers for each post in the table
			DataTable postDataTable = MyRepeaterDataSource.Tables[0];
			postDataTable.Columns.Add(new DataColumn("ReplyNumbers",typeof(string)));
			foreach (DataRow row in postDataTable.Rows)
			{
				row["ReplyNumbers"] = getPostNumberOfReplies(Convert.ToString(row["PostID"]));
			}
				
			MyRepeater.DataSource =  postDataTable;			
			MyRepeater.DataBind();			
			
			DateTime curr = DateTime.Now;
			DateTime start = DateTime.Parse("2005/09/01");

			ArrayList dtList = new ArrayList();
			for (DateTime dt = start; 
				dt <= curr; dt = dt.AddMonths(1))
			{
				dtList.Add(dt);
			}
			dtList.Reverse();

			DataList3.DataSource = dtList;
			DataList3.DataBind();

			DataSet userDS = UserObject.GetUserByID(Convert.ToUInt16(userID));

			DataList5.DataSource = userDS;
			DataList5.DataBind();

			DataList6.DataSource = userDS;
			DataList6.DataBind();

		}

		public string getPostNumberOfReplies(string postID) 
		{
			ForumDataAccess da = new ForumDataAccess();

			return Convert.ToString(da.getPostNumberOfReplies(
				                        Convert.ToUInt16(postID),"blog","Active"));
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.MyRepeater.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.MyRepeater_ItemCommand);

		}
		#endregion

		protected void Calendar2_SelectionChanged(object sender, System.EventArgs e)
		{
			Response.Write(Calendar2.SelectedDate);
			DateTime selectedDT = Calendar2.SelectedDate;
					
			Response.Redirect("bloggerweb.aspx?UserId=" + userID + "&Year=" + selectedDT.Year +
					"&Month=" + selectedDT.Month + "&Day=" + selectedDT.Day);
		}

		private void MyRepeater_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
		
		}

/*		private void Button1_Click(object sender, System.EventArgs e)
		{
			ForumDataAccess da = new ForumDataAccess();
			string forumID = "";

			if (Request.Cookies["Email"] == null) 
			{
				Response.Redirect("index.aspx?message=请先登录");
			}

			DataSet ds = UserObject.GetUser(Request.Cookies["Email"].Value);
			int uid = Convert.ToUInt16(ds.Tables[0].Rows[0]["UserID"]);

			da.InsertPost(Convert.ToUInt16(postID),43,
					"",TextBox3.Text,uid,"","blog",out forumID);

			BindData();
			TextBox3.Text = "";
				 
		}
*/
		
		protected void LinkButton3_Click1(object sender, EventArgs e)
        {
            Response.Redirect("bloggerinput.aspx?UserID=" + userID);
        }

        protected void LinkButton1_Click1(object sender, EventArgs e)
        {
            Response.Redirect("bloggerarticles.aspx?UserID=" + userID);
        }

        protected void LinkButton2_Click1(object sender, EventArgs e)
        {
            Response.Redirect("bloggercomments.aspx?UserID=" + userID);
        }
	}
}
